package com.lin.activity;

import java.util.ArrayList;

import com.lin.utils.DatabaseHelperForChattingMap;
import com.lin.utils.Session;
import com.lin.utils.Session.ResultReceiver;
import com.lin.utils.Session.SignupResult;
import com.lin.views.ListViewForMyFensi.OnItemListViewEventListener;
import com.lin.views.ListViewForMyFensi;
import com.lin.views.ListViewForFocus;
import com.lin.views.ListViewForMyFensi;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.activity.ChatActivity;
import com.yunfeng.supercall.activity.ShareActivity;
import com.yunfeng.supercall.bean.Constants;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.bean.UserBean;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.Toast;

public class MyFensiListActivity extends Activity implements OnClickListener, Constants, OnItemClickListener{

	private ListViewForMyFensi mListView;
	Session mSession;
	UserBean user;
	private FrameLayout mFrameLayoutMore;
	private FrameLayout mFrameLayoutBack;
	ArrayList<SuperFriendsBean> mList;
	private SwipeRefreshLayout mSwipeRefreshLayout;
	private int otherId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_fensi);

		otherId = getIntent().getIntExtra("data", 0);
		user = BaseActivity.getUser(getApplicationContext());


		mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);


		mSwipeRefreshLayout.setColorSchemeResources(R.color.base_orange);

		mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				mSwipeRefreshLayout.setRefreshing(true);
				loadData(otherId);
			}
		});

		mSwipeRefreshLayout.setEnabled(false);



		mSession = Session.getSession();
		mSession.init(getApplicationContext(), true);
		loadData(otherId);

		mListView = (ListViewForMyFensi) findViewById(R.id.listview);
		//		mListView.setOnItemListViewEventListener(this);
		mFrameLayoutMore = (FrameLayout) findViewById(R.id.framelayout_more);
		mFrameLayoutBack = (FrameLayout) findViewById(R.id.framelayout_back);

		mFrameLayoutBack.setOnClickListener(this);
		mFrameLayoutMore.setOnClickListener(this);
		//		mListView.setOnItemListViewEventListener(this);
		mListView.setOnItemClickListener(this);
		mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView absListView, int i) {

			}

			@Override
			public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				if (firstVisibleItem == 0)
					mSwipeRefreshLayout.setEnabled(true);
				else
					mSwipeRefreshLayout.setEnabled(false);
			}
		});
	}
	private void loadData(int otherId) {
		mSession.getFensiList(getApplicationContext(), 0,otherId, new ResultReceiver<ArrayList<SuperFriendsBean>>() {

			@Override
			public void onResult(boolean last, ArrayList<SuperFriendsBean> data) {
				// TODO Auto-generated method stub
				mSwipeRefreshLayout.setRefreshing(false);

				if(data == null) return;
				if(data != null)  {
					mList = data;		
					DatabaseHelperForChattingMap dbHelper3 = new DatabaseHelperForChattingMap(MyFensiListActivity.this, "db", 2);
					dbHelper3.insertSuperFrined(data);
					if(dbHelper3 != null) {
						dbHelper3.close();
						dbHelper3 = null;
					}
					mListView.setDataList(data);
				}
			}

		});
	}
	/*@Override
	public void onItemAvatarLisener(SuperFriendsBean maListInfo, int x, int y) {
		// TODO Auto-generated method stub
		startActivity(new Intent(this, ChatActivity.class)
		.putExtra(KEY_DATA, maListInfo));
		Intent intent = new Intent(this, PersonalDetailCardActivity.class);
		intent.putExtra("data", maListInfo);
		startActivity(intent);
	}
	@Override
	public void onItemClickListener(SuperFriendsBean maListInfo, int x, int y) {
		// TODO Auto-generated method stub
		startActivity(new Intent(this, ChatActivity.class)
		.putExtra(KEY_DATA, maListInfo));
		Intent intent = new Intent(this, PersonalDetailCardActivity.class);
		intent.putExtra("data", maListInfo);
		startActivity(intent);
	}
	@Override
	public void onItemStateListener(final SuperFriendsBean maListInfo, int x, int y) {
		// TODO Auto-generated method stub
		mSession.addFollower(getApplicationContext(), user.getUserid(), maListInfo.getUserid(), new ResultReceiver<SignupResult>() {

			@Override
			public void onResult(boolean last, SignupResult data) {
				// TODO Auto-generated method stub
				if(data == SignupResult.SUCCESS) {
					maListInfo.setIsClicked(1);
					mListView.updateview();
					Toast.makeText(getApplicationContext(), MyFensiListActivity.this.getResources().getString(R.string.add_success), Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getApplicationContext(), MyFensiListActivity.this.getResources().getString(R.string.add_fail), Toast.LENGTH_SHORT).show();
				}
			}

		});
	}
	 */
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if(mFrameLayoutBack == view) {
			finish();
		} else if(mFrameLayoutMore == view) {
			Intent intent = new Intent(this, ShareActivity.class);
			startActivity(intent);
		}
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		SuperFriendsBean maListInfo = mList.get(arg2); 
		Intent intent = new Intent(this, PersonalDetailCardActivity.class);
		intent.putExtra("data", maListInfo);
		startActivity(intent);
	}

}